package ninja9.muzik.dao;

import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * A data access object (DAO) providing persistence and search support for
 * Singer entities. Transaction control of the save(), update() and delete()
 * operations must be handled externally by senders of these methods or must be
 * manually added to each of these methods for data to be persisted to the JPA
 * datastore.
 * 
 * @see ninja9.muzik.dao.Singer
 * @author MyEclipse Persistence Tools
 */
public class SingerDAO implements ISingerDAO {
	// property constants
	public static final String NAME = "name";
	public static final String AVARTAR = "avartar";
	public static final String CAPTION = "caption";

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	/**
	 * Perform an initial save of a previously unsaved Singer entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#persist(Object)
	 * EntityManager#persist} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * SingerDAO.save(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Singer entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Singer entity) {
		EntityManagerHelper.log("saving Singer instance", Level.INFO, null);
		try {
			getEntityManager().persist(entity);
			EntityManagerHelper.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent Singer entity. This operation must be performed
	 * within the a database transaction context for the entity's data to be
	 * permanently deleted from the persistence store, i.e., database. This
	 * method uses the {@link javax.persistence.EntityManager#remove(Object)
	 * EntityManager#delete} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * SingerDAO.delete(entity);
	 * EntityManagerHelper.commit();
	 * entity = null;
	 * </pre>
	 * 
	 * @param entity
	 *            Singer entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Singer entity) {
		EntityManagerHelper.log("deleting Singer instance", Level.INFO, null);
		try {
			entity = getEntityManager().getReference(Singer.class,
					entity.getId());
			getEntityManager().remove(entity);
			EntityManagerHelper.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved Singer entity and return it or a copy of it to
	 * the sender. A copy of the Singer entity parameter is returned when the
	 * JPA persistence mechanism has not previously been tracking the updated
	 * entity. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * entity = SingerDAO.update(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Singer entity to update
	 * @return Singer the persisted Singer entity instance, may not be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public Singer update(Singer entity) {
		EntityManagerHelper.log("updating Singer instance", Level.INFO, null);
		try {
			Singer result = getEntityManager().merge(entity);
			EntityManagerHelper.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public Singer findById(Integer id) {
		EntityManagerHelper.log("finding Singer instance with id: " + id,
				Level.INFO, null);
		try {
			Singer instance = getEntityManager().find(Singer.class, id);
			return instance;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all Singer entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the Singer property to query
	 * @param value
	 *            the property value to match
	 * @return List<Singer> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<Singer> findByProperty(String propertyName, final Object value) {
		EntityManagerHelper.log("finding Singer instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			final String queryString = "select model from Singer model where model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Singer> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Singer> findByAvartar(Object avartar) {
		return findByProperty(AVARTAR, avartar);
	}

	public List<Singer> findByCaption(Object caption) {
		return findByProperty(CAPTION, caption);
	}

	/**
	 * Find all Singer entities.
	 * 
	 * @return List<Singer> all Singer entities
	 */
	@SuppressWarnings("unchecked")
	public List<Singer> findAll() {
		EntityManagerHelper.log("finding all Singer instances", Level.INFO,
				null);
		try {
			final String queryString = "select model from Singer model";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

}